how to create a linked list in java without using util package? [closed]

Posted by aparna on Stack Overflow See other posts from Stack Overflow or by aparna
Published on 2012-03-24T05:09:06Z Indexed on 2012/03/24 5:30 UTC
Read the original article Hit count: 130

Filed under:
|

I do not want to use any of the elements of the UTIL package. I tried but I am not able to get to the next node. Can anyone please help me out? Thanks in advance.

I have put up this much. public class Element { private String s; private Element next; }

    public Element(String s)
    {
        this.s = s;
        this.next = null;
    }

    public void setNext(Element e)
    {
        this.next = e;
    }

    public String getString()
    {
        return this.s;
    }

    public Element getNext()
    {
        return this.next;
    }

    public String toString()
    {
        return "[" + s + "] => ";
    }

    public void add(String s)
    {
        int index = 0;
        System.out.println("Adding at index: " + index);
        Element curr = new Element(s);
        Element e = this.data[index];
        if (e == null)
        {
           this.data[index] = curr;
           return;
        }
        while(e.getNext() != null)
        {
            e = e.getNext();
        }
        e.setNext(curr);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about homework